package com.ruoyi.web.controller.common;

import cn.xuyanwu.spring.file.storage.FileInfo;
import cn.xuyanwu.spring.file.storage.FileStorageService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
public class FileDetailController {

    @Resource
    private FileStorageService fileStorageService;

    /**
     * 上传文件，成功返回文件 url
     */
    @PostMapping("/upload")
    public Map<String, Object> upload(MultipartFile file) {
        FileInfo fileInfo = fileStorageService.of(file).upload();
        if (fileInfo == null) {
            throw new RuntimeException("文件上传失败");
        }
        Map<String,Object> res = new HashMap<>(2);
        res.put("name",file.getName());
        res.put("success",true);
        res.put("url",fileInfo.getUrl());
        return res;
    }
}
